package com.wangdao.mall.service;

import com.wangdao.mall.dao.GoodsDao;
import com.wangdao.mall.dao.GoodsDaoImp;
import com.wangdao.mall.model.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:HaiyangTian
 * @Description:
 * @Date: Created in 20:01 2020/5/1
 * @Modified By:
 */
public class GoodsServiceImp implements GoodsService {

    private GoodsDao goodsDao = new GoodsDaoImp();

    @Override
    public List<Type> getType() {
        return goodsDao.getType();
    }

    @Override
    public List<Goods> getGoodsByType(String typeId) {
        return goodsDao.getGoodsByType(typeId);
    }

    /**
     * 将参数保存到两张表里，所以要在service层中作逻辑
     * 要将goods表和spec表关联起来
     * @param goodsParam
     * @return
     */
    @Override
    public void addGoods(GoodsParam goodsParam) {
        List<Spec> specList = updatePriceAndStockNum(goodsParam);
        int goodsId = goodsDao.addGoods(goodsParam);
        //得到这个商品id然后添加到specList中
        goodsDao.addSpecList(goodsId,specList);
    }

    @Override
    public int addType(Type type) {
        return goodsDao.addType(type);
    }

    @Override
    public int deleteType(String typeId) {
        return goodsDao.deleteType(typeId);
    }

    /**
     * 商品表和规格表中的数据都要删除
     * @param id
     */
    @Override
    public void deleteGoods(String id) {
        goodsDao.deleteGoods(id);
        goodsDao.deleteSpecList(id);
    }

    @Override
    public Map<String, Object> getGoodsInfo(String id) {
        Map<String, Object> goodsMap = new HashMap<>();
        Goods goods = goodsDao.getGoods(id);
        List<Spec> specs = goodsDao.getSpecs(id);
        goodsMap.put("goods",goods);
        goodsMap.put("specs",specs);
        return goodsMap;
    }

    @Override
    public int addSpec(SpecId spec) {
        return goodsDao.addSpec(spec);
    }

    @Override
    public int deleteSpec(SpecId spec) {
        return goodsDao.deleteSpec(spec);
    }

    @Override
    public void updateGoods(GoodsParam goodsParam) {
        //如果改动了规格，那么对应的业务逻辑展示也有可能变动
        //所以在更新商品数据前先修改展示的价格和数量数据
        updatePriceAndStockNum(goodsParam);
        goodsDao.updateGoods(goodsParam);
    }

    /**
     * 封装user对象到message中
     * @return
     */
    @Override
    public List<Message> getNoReplyMsg() {
        List<Message> messages = goodsDao.getNoReplyMsg();
        //userName不能直接获取，所以遍历数组依次封装数据
        for (Message message : messages) {
            MessageGoods messageGoods = new MessageGoods();
            MessageUser messageUser = new MessageUser();
            messageGoods.setName(goodsDao.getGoodsName(message.getGoodsId()));
            messageUser.setName(goodsDao.getUserName(message.getUserId()));
            message.setGoods(messageGoods);
            message.setUser(messageUser);
        }
        return messages;
    }

    /**
     * 封装回复留言的数据
     * @return
     */
    @Override
    public List<ReplyMessage> getRepliedMsg() {
        List<ReplyMessage> messages = goodsDao.getRepliedMsg();
        for (ReplyMessage message : messages) {
            MessageGoods messageGoods = new MessageGoods();
            MessageUser messageUser = new MessageUser();
            //这个时间是回复的时间
            //因为响应体里提问和回复的时间重名，所以只好重新赋值
            Reply reply = goodsDao.getReply(message.getId());
            messageGoods.setName(goodsDao.getGoodsName(message.getGoodsId()));
            messageUser.setName(goodsDao.getUserName(message.getUserId()));
            message.setCreatetime(reply.getTime());
            message.setGoods(messageGoods);
            message.setUser(messageUser);
        }
        return messages;
    }

    @Override
    public void reply(ReplyMessage replyMessage) {
        goodsDao.reply(replyMessage);
    }

    @Override
    public ClientGoods getClientGoodsInfo(String id) {
        ClientGoods goods = goodsDao.getGoodsInfo(id);
        List<Spec> specs = goodsDao.getSpecs(id);
        goods.setSpecs(specs);
        return goods;
    }

    @Override
    public List<ClientMessage> getGoodsMsg(String id) {
        List<ClientMessage> messages = goodsDao.getGoodsMsg(id);
        for (ClientMessage message : messages) {
            Reply reply = goodsDao.getReply(message.getId());
            message.setAsker(goodsDao.getUserName(goodsDao.getUserId(message.getId())));
            System.out.println(goodsDao.getCreateTime(message.getId()));
            message.setTime(goodsDao.getCreateTime(message.getId()));
            message.setReply(reply);
        }
        return messages;
    }

    @Override
    public int askGoodsMsg(AskMessage message) {
        return goodsDao.askGoodsMsg(message);
    }

    /**
     * 按照业务逻辑需求获得商品展示的价格和数量
     * @param goodsParam
     */
    private List<Spec> updatePriceAndStockNum(GoodsParam goodsParam){
        List<Spec> specList = goodsParam.getSpecList();
        //price得到的是规格中的最小值
        double price = specList.get(0).getUnitPrice();
        //取最大值(这两个根据业务逻辑都可以做出改变)
        int stockNum = specList.get(0).getStockNum();
        //遍历soeclist然后比较得出你想要的值
        for (int i = 1; i < specList.size(); i++) {
            if (price > specList.get(i).getUnitPrice()){
                price = specList.get(i).getUnitPrice();
            }
            if (stockNum < specList.get(i).getStockNum()){
                stockNum = specList.get(i).getStockNum();
            }

        }
        //设置一下，这样查询的时候就可以取到这个值了
        goodsParam.setPrice(price);
        goodsParam.setStockNum(stockNum);
        return specList;
    }
}
